package com.papercutting.platform.controller.admin;

import com.papercutting.platform.common.Result;
import com.papercutting.platform.entity.TutorialStep;
import com.papercutting.platform.service.TutorialStepService;
import com.papercutting.platform.service.TutorialService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据初始化控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/admin/data")
@RequiredArgsConstructor
public class DataInitController {
    
    private final TutorialStepService tutorialStepService;
    private final TutorialService tutorialService;
    
    /**
     * 为教程7添加步骤数据
     */
    @PostMapping("/init-tutorial-7-steps")
    public Result<String> initTutorial7Steps() {
        try {
            log.info("开始为教程7添加步骤数据");
            
            // 创建步骤数据
            List<TutorialStep> steps = new ArrayList<>();
            
            TutorialStep step1 = new TutorialStep();
            step1.setTutorialId(7L);
            step1.setStepNumber(1);
            step1.setTitle("了解教程结构");
            step1.setDescription("首先了解本教程的整体结构和学习目标，制定学习计划。");
            step1.setImageUrl("/images/steps/step7_1.jpg");
            step1.setTips("制定合理的学习时间安排，有助于更好地掌握技能。");
            step1.setSortOrder(1);
            step1.setCreateTime(LocalDateTime.now());
            step1.setUpdateTime(LocalDateTime.now());
            step1.setDeleted(false);
            steps.add(step1);
            
            TutorialStep step2 = new TutorialStep();
            step2.setTutorialId(7L);
            step2.setStepNumber(2);
            step2.setTitle("准备学习环境");
            step2.setDescription("准备一个安静舒适的学习环境，确保有足够的光线和空间。");
            step2.setImageUrl("/images/steps/step7_2.jpg");
            step2.setTips("良好的学习环境是成功学习的基础。");
            step2.setSortOrder(2);
            step2.setCreateTime(LocalDateTime.now());
            step2.setUpdateTime(LocalDateTime.now());
            step2.setDeleted(false);
            steps.add(step2);
            
            TutorialStep step3 = new TutorialStep();
            step3.setTutorialId(7L);
            step3.setStepNumber(3);
            step3.setTitle("基础知识学习");
            step3.setDescription("学习相关的基础理论知识，为实践操作打下坚实基础。");
            step3.setImageUrl("/images/steps/step7_3.jpg");
            step3.setTips("理论与实践相结合，学习效果会更好。");
            step3.setSortOrder(3);
            step3.setCreateTime(LocalDateTime.now());
            step3.setUpdateTime(LocalDateTime.now());
            step3.setDeleted(false);
            steps.add(step3);
            
            TutorialStep step4 = new TutorialStep();
            step4.setTutorialId(7L);
            step4.setStepNumber(4);
            step4.setTitle("实践操作练习");
            step4.setDescription("根据教程内容进行实际操作练习，熟悉基本技能。");
            step4.setImageUrl("/images/steps/step7_4.jpg");
            step4.setTips("多练习，熟能生巧。");
            step4.setSortOrder(4);
            step4.setCreateTime(LocalDateTime.now());
            step4.setUpdateTime(LocalDateTime.now());
            step4.setDeleted(false);
            steps.add(step4);
            
            TutorialStep step5 = new TutorialStep();
            step5.setTutorialId(7L);
            step5.setStepNumber(5);
            step5.setTitle("作品创作");
            step5.setDescription("运用所学知识创作自己的作品，发挥创意和想象力。");
            step5.setImageUrl("/images/steps/step7_5.jpg");
            step5.setTips("勇于创新，展现个人风格。");
            step5.setSortOrder(5);
            step5.setCreateTime(LocalDateTime.now());
            step5.setUpdateTime(LocalDateTime.now());
            step5.setDeleted(false);
            steps.add(step5);
            
            TutorialStep step6 = new TutorialStep();
            step6.setTutorialId(7L);
            step6.setStepNumber(6);
            step6.setTitle("总结反思");
            step6.setDescription("总结学习过程中的收获和不足，为进一步学习做准备。");
            step6.setImageUrl("/images/steps/step7_6.jpg");
            step6.setTips("及时总结有助于巩固所学知识。");
            step6.setSortOrder(6);
            step6.setCreateTime(LocalDateTime.now());
            step6.setUpdateTime(LocalDateTime.now());
            step6.setDeleted(false);
            steps.add(step6);
            
            // 保存步骤
            for (TutorialStep step : steps) {
                tutorialStepService.save(step);
            }
            
            log.info("教程7的步骤数据添加完成，共添加{}个步骤", steps.size());
            
            return Result.success("教程7的步骤数据添加成功，共添加" + steps.size() + "个步骤");
            
        } catch (Exception e) {
            log.error("添加教程7步骤数据失败", e);
            return Result.error("添加步骤数据失败: " + e.getMessage());
        }
    }
}

